package com.kanshu.ksgb.fastread.doudou.common.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewParent;

/**
 * RecyclerView滑动到最后一个item时,
 * 继续滑动将会引起外层的ViewPager的滑动,这个控件可以避免此类情况
 * Created by wcy on 18/6/14.
 */

public class MyHorizontalRecyclerView extends RecyclerView {
    public MyHorizontalRecyclerView(Context context) {
        super(context);
    }

    public MyHorizontalRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyHorizontalRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        ViewParent parent = this;
        // 循环查找ViewPager, 请求ViewPager不拦截触摸事件
        while(parent != null && !((parent = parent.getParent()) instanceof ViewPager)){
            // nop
        }
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
        }
        return super.dispatchTouchEvent(ev);
    }
}
